<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account_groups extends Admin_Controller {

	protected $table = ADMIN_GROUP_TABLE; // Also configed in model
	
	// Config List view
	protected $list_view_order_name = 'name';
	protected $list_view_order_direction = 'ASC';
	protected $list_view_searched_fields = array(
			'name'			=>	'name',
		);
		
	protected $fields =  array(
			'status'		=>	array(
								'text'	=>	'Status',
								'type'	=>	'checkbox',
								'form_view'=>array(
									'type'				=>	'checkbox',
									'func'				=>	''
								)
							),
			'name'		=>	array(
								'text'		=>	'Group-Name',
								'type'		=>	'text',
								'list_view'=>array(
									'is_show_link'	=>	true
								),
								'form_view'=>array(
									'type'	=>	'text',
									'func'	=>	''
								)
							),
			);
	/** End: Config **/
	/*------------------------------------ Admin Control Panel ------------------------------------*/
	
	
	public function admincp_update($id=0){
		admin_check_permission($this->module_id,'r',true);
		$result = array();
		if($id!=0){
			$result = $this->model->getDetailManagement($id);
		}
		
		$this->load->model('manager_modules/manager_modules_model');
		$list_modules = $this->manager_modules_model->list_module();
		$data = array(
			'result'=>$result,
			'list_modules'=>$list_modules,
			'module'=>$this->module,
			'id'=>$id
		);
		$this->template->write_view('content','ajax_editContent',$data);
		$this->template->render();
	}

	public function admincp_save(){
		admin_check_permission($this->module_id,'w',true);
		if($_POST){
			//Permission default
			$result_perm = '';
			$default_perm = '000|';
			if($this->input->post('read0Admincp')=='on'){
				$default_perm .= 'r';
			}else{
				$default_perm .= '-';
			}
			if($this->input->post('write0Admincp')=='on'){
				$default_perm .= 'w';
			}else{
				$default_perm .= '-';
			}
			if($this->input->post('delete0Admincp')=='on'){
				$default_perm .= 'd';
			}else{
				$default_perm .= '-';
			}
			
			//Permission list module
			$this->load->model('manager_modules/manager_modules_model');
			$list_modules = $this->manager_modules_model->list_module();
			$perm = '';
			
			foreach($list_modules as $module){
				$normalized_module_id = admin_normalize_module_id($module->id);
				
				$perm .= ','.$normalized_module_id.'|';
				if($this->input->post('read'.$normalized_module_id.'Admincp')=='on'){
					$perm .= 'r';
				}else{
					$perm .= '-';
				}
				if($this->input->post('write'.$normalized_module_id.'Admincp')=='on'){
					$perm .= 'w';
				}else{
					$perm .= '-';
				}
				if($this->input->post('delete'.$normalized_module_id.'Admincp')=='on'){
					$perm .= 'd';
				}else{
					$perm .= '-';
				}
			}
			$result_perm = $default_perm.$perm;
			if($this->model->saveManagement($result_perm)){
				if($this->input->post('hiddenIdAdmincp')!=0){
					$this->load->model('accounts/accounts_model');
					$list_account = $this->accounts_model->list_accounts(1);
					foreach($list_account as $val){
						if($val->group_id == $this->input->post('hiddenIdAdmincp')){
							$this->db->where('id',$val->id);
							$this->db->update(ADMIN_USER_TABLE,array('permission'=>substr($perm,1)));
						}
					}
				}
				print 'success';
				exit;
			}
		}
	}
	
	
	/*------------------------------------ End Admin Control Panel --------------------------------*/
	
	
	/*------------------------------------ FRONTEND ------------------------------------*/
	
	/*------------------------------------ End FRONTEND --------------------------------*/
}